﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Common
{
    [Serializable]
    public class Settings
    {
        //DataBase
        private string _databasePath = string.Empty;
        private string _databasePassword = string.Empty;

        //GPS
        private bool _enableGPS = false;
        private string _portGPS = string.Empty;

        //ImportExportData
        private string importPath = string.Empty;
        private string exportPath = string.Empty;

        //ArcImportExportData
        private bool isSaveFiles = false;
        private string arcImportPath = string.Empty;
        private string arcExportPath = string.Empty;

        //RemainingGoods
        private bool checkRemainingGoods = true;
        private bool visibleNullGoods = false;
        
        [NonSerialized]
        protected static Settings _instance = null;


        public string DatabasePath 
        {
            get
            {
                return _instance._databasePath;
            }
            set
            {
                _instance._databasePath = value;
            } 
        }

        public string DatabasePassword
        {
            get
            {
                return _instance._databasePassword;
            }
            set
            {
                _instance._databasePassword = value;
            }
        }

        public bool EnableGPS
        {
            get
            {
                return _instance._enableGPS;
            }
            set
            {
                _instance._enableGPS = value;
            }
        }

        public string PortGPS
        {
            get
            {
                return _instance._portGPS;
            }
            set
            {
                _instance._portGPS = value;
            }
        }
        
        public string ImportPath
        {
            get
            {
                return importPath;
            }
            set
            {
                importPath = value;
            }
        }

        public string ExportPath
        {
            get
            {
                return exportPath;
            }
            set
            {
                exportPath = value;
            }
        }

        public string ArcImportPath
        {
            get
            {
                return arcImportPath;
            }
            set
            {
                arcImportPath = value;
            }
        }
        public string ArcExportPath
        {
            get
            {
                return arcExportPath;
            }
            set
            {
                arcExportPath = value;
            }
        }

        public bool IsSaveFiles
        {
            get 
            {
                return isSaveFiles;
            }
            set
            {
                isSaveFiles = value;
            }
        }

        public bool CheckRemainingGoods
        {
            get
            {
                return checkRemainingGoods;
            }
            set
            {
                checkRemainingGoods = value;
            }
        }

        public bool VisibleNullGoods
        {
            get 
            {
                return visibleNullGoods;
            }
            set
            {
                visibleNullGoods = value;
            }
        }

        public Settings()
        {
            _instance = this;
        }

        public static Settings GetInstance ()
        {
            if (_instance == null)
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof (Settings));
                    FileStream fstream =
                        new FileStream(
                            Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Settings.xml",
                            FileMode.Open);
                    try
                    {
                        _instance = (Settings) serializer.Deserialize(fstream);
                    }
                    finally
                    {
                        fstream.Close();
                    }
                }
                catch(Exception)
                {
                    _instance = new Settings();
                    throw new Exception("Помилка при завантаженні настройок !");
                }
            }
            return _instance;
        }

        public void Save ()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            FileStream fstream =
                new FileStream(
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Settings.xml",
                    FileMode.Create);
            try
            {
                serializer.Serialize(fstream, _instance);
            }
            finally
            {
                fstream.Close();
            }
        }
    }
}
